package kr.hyosang.android.penfo;

import kr.hyosang.AsyncHttp.AsyncHttpListener;
import kr.hyosang.android.penfo.adapter.MainPageListAdapter;
import kr.hyosang.android.penfo.data.Article;
import kr.hyosang.android.penfo.data.MainPageData;
import kr.hyosang.android.penfo.parser.MainPageParser;
import kr.hyosang.android.penfo.processor.BaseProcessor;
import kr.hyosang.android.penfo.processor.ProcessorListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;

public class MainActivity extends Activity implements ProcessorListener<MainPageData>
{
    AsyncHttpListener mainPageListener = null;
    ListView mMainArticleList;
    MainPageListAdapter mListAdapter;
    Handler mHandler;
    BaseProcessor<Void, MainPageData> mProcessor;
    HttpImage choiceLoader;
    HttpImage [] featuredLoader;
    ScrollView mMainScrollView;
    HorizontalScrollView mFeatureScroll;
    
    
    @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        SharedObject.getInstance().init(this.getApplicationContext());
        
        mMainArticleList = (ListView)findViewById(R.id.mainArticleList);
        mListAdapter = new MainPageListAdapter();
        mMainArticleList.setAdapter(mListAdapter);
        mMainArticleList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
            	Article article = (Article)mListAdapter.getItem(arg2);
            	if(article != null) {
            		Class<?> boardActivity = Article.getBoardActivity(article);
            		if(boardActivity != null) {
            			Intent i = new Intent(MainActivity.this, boardActivity);
            			i.putExtra("no", article.no);
            			startActivity(i);
            		}else {
            			Log.d("penfo", "Board is null");
            		}
            	}else {
            		Log.d("penfo", "Article is null");
            	}
            }
        });
        
        mMainScrollView = (ScrollView)findViewById(R.id.mainScrollView);
        mFeatureScroll = (HorizontalScrollView)findViewById(R.id.mainFeatures);
        
        refresh();
        
    }
    
    public void refresh() {
        //Load mainpage
        mProcessor = new BaseProcessor<Void, MainPageData>();
        mProcessor.setListener(this);
        mProcessor.setParser(new MainPageParser());
        mProcessor.execute(Definition.URL.MAIN_PAGE);
    }
        
    
	@Override
	public void onComplete(MainPageData data) {
        mListAdapter.setData(data);
        mListAdapter.notifyDataSetChanged();
        
        mMainArticleList.setVisibility(View.GONE);
        mMainArticleList.setVisibility(View.VISIBLE);

        LayoutParams lp = mMainArticleList.getLayoutParams();
        lp.height = mListAdapter.getTotalHeight();
        mMainArticleList.setLayoutParams(lp);
        
        ImageView choiceView = (ImageView)findViewById(R.id.choiceImage);
        choiceView.setPadding(10, 10, 10, 10);
        choiceLoader = new HttpImage(choiceView, Definition.URL.TOP + data.choiceImage);
        choiceLoader.setLayoutScaleType(HttpImage.SCALETYPE_MAXIMUM_WIDTH);
        choiceLoader.load();
        
        //featured picture
        featuredLoader = new HttpImage[data.featured.size()];
        Context context = SharedObject.getInstance().mAppContext;
        int nWidth = (int)(SharedObject.getInstance().fDensity * 80f);
        LinearLayout parentView = (LinearLayout)findViewById(R.id.mainFeaturesList);

        for(int i=0;i<featuredLoader.length;i++) {
            ImageView imageView = new ImageView(context);
            lp = new LayoutParams(nWidth, nWidth);
            imageView.setLayoutParams(lp);
            imageView.setPadding(5, 0, 5, 0);
            
            parentView.addView(imageView);
            
            featuredLoader[i] = new HttpImage(imageView, Definition.URL.TOP + data.featured.get(i));
            featuredLoader[i].load();
        }		
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, Definition.MENU_ID.LOGIN, 0, R.string.login);
        menu.add(0, Definition.MENU_ID.LOGOUT, 0, R.string.logout);
        
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean bLogin = SharedObject.getInstance().bLogin;
        
        menu.findItem(Definition.MENU_ID.LOGIN).setEnabled(!bLogin);
        menu.findItem(Definition.MENU_ID.LOGOUT).setEnabled(bLogin);
        
        return super.onPrepareOptionsMenu(menu);
        
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == Definition.MENU_ID.LOGIN) {
            //로그인 액티비티 시작
            Intent i = new Intent(this, LoginActivity.class);
            startActivityForResult(i, Definition.ACTIVITY_LOGIN);
        }else if(item.getItemId() == Definition.MENU_ID.LOGOUT) {
            //로그아웃 => ZBSESSIONID 삭제하고 새로고침
            SharedObject.getInstance().logout();
            refresh();
        }
        
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if(requestCode == Definition.ACTIVITY_LOGIN) {
            if(resultCode == Definition.RESULT_OK) {
                refresh();
            }
        }
    }    

    
}
